#include <iostream>


class Base {
public:
    virtual ~Base() {
        std::cout << "Deconstruct in Base " << std::endl;
    } // 虚析构函数
    virtual void func() {}
};

class Derived : public Base {
public:
    ~Derived() override {
        std::cout << "Deconstruct in derived " << std::endl;
    } // 派生类中实现虚析构函数
    void func() override {}
};

int main() {
    Base* basePtr = new Derived;

    Derived* derivedPtr = dynamic_cast<Derived*>(basePtr);

    if (derivedPtr) {
        derivedPtr->func();
    } else {
        std::cout << "Dynamic cast failed" << std::endl;
    }

    delete basePtr; // 这里会调用虚析构函数
    // delete derivedPtr;//再次delete basePtr指向的区域，segmentation fault
    return 0;
}
